/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.krazevina.kar;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Vector;
import javax.microedition.rms.RecordStore;

/**
 *
 * @author XXX
 */

public class TaxiResource {
    public Vector prov;
    
    Taxi getTaxi(int id){
        Taxi t;
        Prov p;
        for(int i=0;i<prov.size();i++){
            p = (Prov)prov.elementAt(i);
            for(int j=0;j<p.taxi.size();j++){
                t = (Taxi)p.taxi.elementAt(j);
                if(t.ind==id)return t;
            }
        }
        return null;
    }
    
    void init() throws UnsupportedEncodingException, IOException{
        prov = new Vector();
        int index=0;
        Prov pro;Taxi t;
        InputStream inps = getClass().getResourceAsStream("/taxi.txt");
        InputStreamReader ir = new InputStreamReader(inps,"UTF-8");
        BufferedReader in = new BufferedReader(ir);
        String res = "";
        String r;
        while((r = in.readLine(true))!=null){
            res+=r+"\n";
        }
        String[]pr = split(res,"$");
        String[]tx;
        for(int i=0;i<pr.length;i++){
            pro = new Prov();
            tx = split(pr[i],"\n");
            try{
                pro.name = tx[1];
                for(int j=2;j<tx.length-1;){
                    try{
                        t = new Taxi();
                        t.name = tx[j++];
                        t.num = tx[j++];
                        t.ind = index++;
                        pro.taxi.addElement(t);
                    }catch(Exception e){
                        System.out.println(""+j+"/"+tx.length+"/"+pro.name);
                    }
                }
                prov.addElement(pro);
            }catch(Exception e){
            }
        }
        try {
            byte[]b = new byte[50];String s;
            String []s1;int provod;
            RecordStore rs = RecordStore.openRecordStore("dataTaxi", false);
            for(int i=0;i<rs.getNextRecordID();i++){
                try{
                    b = rs.getRecord(i);
                    s = new String(b);
                    s1 = split(s, ",");
                    Taxi tax = new Taxi();
                    if(s1[0].length()>0)tax.ind = Integer.parseInt(s1[0]);
                    else tax.ind = -1;
                    tax.name = s1[1];
                    tax.num = s1[2];
                    if(s1[3].length()>0)provod = Integer.parseInt(s1[3]);
                    else provod = -1;
                    
                    if(tax.ind>=0){
                        Taxi ta = getTaxi(tax.ind);
                        ta.name = tax.name;
                        ta.num = tax.num;
                    }else{
                        ((Prov)prov.elementAt(provod)).taxi.addElement(tax);
                    }
                }catch(Exception exe){
                }
            }
            Taxi.getBookmark(this);
        } catch (Exception ex) {
            ex.printStackTrace();
        } 
    }
    public static String[] split(String original,String separator) {
        Vector nodes = new Vector();
        int index = original.indexOf(separator);
        while(index>=0) {
            nodes.addElement( original.substring(0, index) );
            original = original.substring(index+separator.length());
            index = original.indexOf(separator);
        }
        nodes.addElement( original );
        String[] result = new String[ nodes.size() ];
        if( nodes.size()>0 ) {
            for(int loop=0; loop<nodes.size(); loop++)
                result[loop] = (String)nodes.elementAt(loop);
        }
        return result;
    }
    
}
